
// const baseUrl = 'https://test.zhgd.com';//测试接口地址
const baseUrl = 'https://mpapi.zhgd.com';//正式接口地址

module.exports = {
	/*
	 * url:请求的接口地址
	 * methodType:请求方式
	 * data: 要传递的参数
	 */
	request: function (url, method="GET", data={}) {
		let fullUrl = `${baseUrl}${url}`

		wx.showLoading({ title: '加载中' })
		return new Promise((resolve, reject) => {
			wx.request({
				url: fullUrl,
				method,
				data,
				header: {
					'content-type': 'application/json', // 默认值
					'cookie': wx.getStorageSync("sessionid"), //读取cookie,
				},
				success: (res) => {
					res.statusCode === 200 ? resolve(res.data) : reject(res.data)
					if (res.cookies.length > 0) {
						for (var cookieStr of res.cookies) {
							var idx = cookieStr.indexOf("SESSION=");
							if (idx != -1) {
								var P = cookieStr.substr(idx)
								wx.setStorageSync("sessionid", P.split(";")[0])
								break
							}
						}
					}
				},
				fail: (err) => {
					wx.showToast({
						title: '接口请求错误',
						icon: 'none',
					})
					reject(err)
				},
				complete: () => {
					wx.hideLoading()
				},
			})
		})
	}
}
